<?php
class IndexAction extends AdminbaseAction
{
    public function index()
    {
 		
    $where="status='0' AND sending_status='0'";
		$model_order=M("order");
    	$alist = $model_order->where($where)->findAll();
    	if ($alist){
    		$this->sendemail($alist);
    	}
    $this->display();
    }
    
    
     public function sendemail($alist){
     		 foreach($alist as $k=>$val){
	    	$EmailAddress=$val['email'];
	    	$currency=$val['currency'];
	    	$orderid=$val['sn'];
	    	$id=$val['id'];
	    	$val['productlist']=unserialize($val['productlist']);
	    	$map['domain']=$val['domain'];
	    	$map['currency']=$val['currency'];
	    	

			$siteinfo=M('website')->where($map)->find();
			$etemplate= $siteinfo['etemplate'];
     		 if($val['num']>1){
				$postage['logisticsprice1'] = $siteinfo['postprice2'];
				$postage['logisticsprice2'] = $siteinfo['postprice4'];
			}else{
				$postage['logisticsprice1'] = $siteinfo['postprice1'];
				$postage['logisticsprice2'] = $siteinfo['postprice3'];
			}
			if ($val['logistics']=='Air Mail'){
				$logistic=$postage['logisticsprice1'];
			}else {
				$logistic=$postage['logisticsprice2'];
			}
			
			$a=array();
			$a=$val['productlist'];
			
			/**
			 * paypal
			 */
     		 if($paypal=M('payment')->find()){
			$paypalname=$paypal['yourinfo'];
			}else{die('has not paypal account');}
			$payarray=array();
			$payarray['first_name'] =$val['firstname'];
			$payarray['last_name'] =$val['lastname'];
			$payarray['address1'] =$val['address1'];
			$payarray['address2'] =$val['address2'];
			$payarray['zip'] =$val['zipcode'];
			$payarray['zip2'] =$val['zipcode2'];
			$payarray['city'] =$val['city'];
			$payarray['state'] =$val['state'];
			$payarray['client_countryship'] =$val['country1'];
			$payarray['night_phone_a'] =$val['night_phone_a'];
			$payarray['night_phone_b'] =$val['night_phone_b'];
			$payarray['night_phone_c'] =$val['night_phone_c'];
			$payarray['email'] =$val['email'];
			$payarray['client_remark'] =$val['note'];
			$payarray['country'] =$val['country'];
			$payarray['destination'] =$val['destination'];
			$payarray['transportation'] =$val['logistics'];
			$payarray['logisticsprice'] =$val[''];
			$payarray['currency_code'] =$val['currency'];
			
			
			
			$_url_1='https://www.sandbox.paypal.com/cgi-bin/webscr';// 沙盒地址 
			$_url_2='https://www.paypal.com/cgi-bin/webscr';//  正式地址
			$reurl='http://'.$_SERVER['HTTP_HOST'].'/index.php?m=Index&a=thanks';  //返回的页面
			$_notify_url="http://".$_SERVER['HTTP_HOST']."/index.php?m=Index&a=ipn"; //验证页面
			
			$pay='
			<form action="'.$_url_2.'" method="post" id="form_paypal" name="form_paypal" target= "_blank ">';
			
			
			foreach($payarray as $_k=>$_v){
				$pay.='<input type="hidden" name="'.$_k.'" value="'.$_v.'">';
			}
			
			
			$pay.='
			
			<input type="hidden" name="cmd" value="_cart">
			<input type="hidden" name="upload" value="1">
			
			
			<input type="hidden" name="lc" value="'.$payarray['country'].'">
			<input type="hidden" name="business" value="'.$paypalname.'">';
			
			
			//卢布美元换算传值
			if($payarray['country']=="RU"){
				$i=0;
				foreach($a as $k=>$val_a){
				$i++;
				$pay.='
				<input type="hidden" name="item_name_'.$i.'" value="'.$val_a['productcode'].'">
				<input type="hidden" name="amount_'.$i.'" value="'.round($val_a['price']*0.035,2).'">
				<input type="hidden" name="quantity_'.$i.'" value="'.$val_a['num'].'">
				<input type="hidden" name="shipping_'.$i.'" value="'.round($logistic*0.035,2).'" />
				<input type="hidden" name="shipping2_'.$i.'" value="'.round($logistic*0.035,2).'" />';
				
				//round($_POST['logisticsprice']*0.035,2)
				}
			}else {
				$i=0;
				foreach($a as $k=>$val_a){
				$i++;
				$pay.='
				<input type="hidden" name="item_name_'.$i.'" value="'.$val_a['productcode'].'">
				<input type="hidden" name="amount_'.$i.'" value="'.$val_a['price'].'">
				<input type="hidden" name="quantity_'.$i.'" value="'.$val_a['num'].'">
				<input type="hidden" name="shipping_'.$i.'" value="'.$logistic.'" />
				<input type="hidden" name="shipping2_'.$i.'" value="'.$logistic.'" />';
				
				}
			
			}
			
			
			$pay.='
			<input type="hidden" name="currency_code" value="'.$payarray['currency_code'].'">
			<input type="hidden" name="return" value="'.$reurl.'">
			<input type="hidden" name="invoice" value="'.$orderid.'">
			<input type="hidden" name="charset" value="utf-8">
			<input type="hidden" name="no_shipping" value="2">
			<input type="hidden" name="no_note" value="0">
			<input type="hidden" name="notify_url" value="'.$_notify_url.'">
			<input type="hidden" name="rm" value="2">
			<input type="hidden" name="cancel_return" value="'.$reurl.'">
			';
		
		
			
			/**
			 * 模版
			 */
			foreach($a as $k=>$val_price){
			$a_price+=$val_price['n_price'];
			}
			//print_r($logistic);
			//echo "<br/>";
			//print_r($a_price+$logistic*$val['num']);
			$username=$val['firstname'].$val['lastname'];
			$this->assign('currency',$payarray['currency_code']);
			$this->assign('domain',$map['domain']);
			$this->assign('username',$username);
			$this->assign('address1',$val['address1']);
			$this->assign('address2',$val['address2']);
			$this->assign('zipcode',$val['zipcode']);
			$this->assign('city',$val['city']);
			$this->assign('country1',$val['country1']);
			$this->assign('email',$val['email']);
			$this->assign('pay',$pay);
			$this->assign('logistic',$logistic);
			$this->assign ('a_prices', $val['price'] );//总价
			$this->assign('a',$a);
			$this->assign('orderid',$orderid);
	    	if(!empty($etemplate)){
					$etemplates='Yourphp/Tpl/Default/Public/emailtemplate/'.$etemplate;
					//print_r($etemplates);
					if(file_exists($etemplates)){$context=$this->fetch ($etemplates);}
			}
			if(!empty($EmailAddress)){
				$ordertitle='Order alert:'.$orderid;
				if(stripos($EmailAddress,',')){
						@set_time_limit(0);
						$addressee=explode(',',$EmailAddress);
							//print_r($addressee);
						foreach($addressee as $val){
								sendmail($val,$ordertitle,$context);
								

								$data['sending_status']= '1';
								$model_order=M("order");
								$model_order->where('id='.$id)->save($data);
																
								
							}
				}else{
							sendmail($EmailAddress,$ordertitle,$context);
							$data['sending_status']= '1';
							$model_order=M("order");
							$model_order->where('id='.$id)->save($data); 
				}
			}
		}
     }
}
?>